package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.domain.Employee;
import cn.wolfcode.education.query.AttentanceQuery;
import cn.wolfcode.education.service.IAttentanceService;
import cn.wolfcode.education.service.IEmployeeService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("attentance")
public class AttentanceController {
    @Autowired
    private IAttentanceService attentanceService;

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("")
    public String index(){
        return "attentance";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(AttentanceQuery qo){
        return attentanceService.query(qo);
    }

    //签到
    @RequestMapping("saveForSignOn")
    @ResponseBody
    public JSONResult saveForSignOn(){
        JSONResult jsonResult = new JSONResult();
        //获取到当前用户
        Employee nowUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        Employee employee = employeeService.getByUsername(nowUser.getUsername());
        nowUser.setId(employee.getId());
        try {
            attentanceService.saveForSignOn(nowUser);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("已经签过了!签再多也不会送女朋友!");
        }
        return jsonResult;
    }

    //签退
    @RequestMapping("saveForSignOut")
    @ResponseBody
    public JSONResult saveForSignOut(){
        JSONResult jsonResult = new JSONResult();
        //获取到当前用户
        Employee nowUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        Employee employee = employeeService.getByUsername(nowUser.getUsername());
        nowUser.setId(employee.getId());
        try {
            attentanceService.saveForSignOut(nowUser);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("已经签退了!再不赶快回去该跪搓衣板,遥控器,方便面了!");
        }
        return jsonResult;
    }

    //补签
    @RequestMapping("saveForSignUp")
    @ResponseBody
    public JSONResult saveForSignUp(){
        JSONResult jsonResult = new JSONResult();
        //获取到当前用户
        Employee nowUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        Employee employee = employeeService.getByUsername(nowUser.getUsername());
        nowUser.setId(employee.getId());
        try {
            attentanceService.saveForSignUp(nowUser);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("今天已经签到过!神经过敏了吧?还跑回来补签!");
        }
        return jsonResult;
    }





}
